/*
 This file is part of ParagliderDesk.
 Copyright (C) 2009 Benjamin Loulier 
 
 ParagliderDesk is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 ParagliderDesk is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with ParagliderDesk.  If not, see <http://www.gnu.org/licenses/>.
 
 File: InstrumentViewController.m
 */

#import "InstrumentViewController.h"

#define RADIANS( degrees ) ( degrees * M_PI / 180 )


@implementation InstrumentViewController

//Synthetize outlets for the view
@synthesize latitudeLabel, longitudeLabel, groundSpeedLabel, verticalSpeedLabel, glideRatioLabel, altitudeLabel, course;
@synthesize groundSpeed, verticalSpeed, glideRatio, altitude;
/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [[CLController sharedInstance] setDelegate:self];
	[[[CLController sharedInstance] locationManager] startUpdatingLocation];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}

- (void)initStrings {
	latitudeLabel = @"NA";
	longitudeLabel = @"NA";
	altitudeLabel = @"NA";
	groundSpeedLabel = @"NA";
	verticalSpeedLabel = @"NA";
	glideRatioLabel = @"NA";
}

- (void)refresh {
	//Show altitude
	[altitude setText:altitudeLabel];
	//Show groundSpeed
	[groundSpeed setText:groundSpeedLabel];
	//Show verticalSpeed
	[verticalSpeed setText:verticalSpeedLabel];
	//Show glideRatio
	[glideRatio setText:glideRatioLabel];
	//Rotate compass
	[compassView setTransform:CGAffineTransformMakeRotation(-RADIANS(course))];
	
}

#pragma mark ---- delegate methods for the MyCLController class ----

- (void)newLocationUpdate {
	//We initialize the strings displayed
	[self initStrings];
	if ([[CLController sharedInstance] twoDFix]) {
		Location *currentLocation = (Location *) [[[CLController sharedInstance] locationHistoric] objectAtIndex:0];
		//We set the course
		if(!signbit([currentLocation course])) {
			course = [currentLocation course];
		}
		else course = 0;
		//We then set the groundSpeed
		if(!signbit([[currentLocation coreLocationOutput] speed])) {
			groundSpeedLabel = [NSString stringWithFormat:@"%.1f", [currentLocation groundSpeedKMH]];
		}
		if ([[CLController sharedInstance] threeDFix]) {
			//We set the altitude
			if(!signbit([[currentLocation coreLocationOutput] altitude])) {
				altitudeLabel = [NSString stringWithFormat:@"%.0fm", [currentLocation altitude]];
			}
			//We set the verticalSpeed
			if([currentLocation verticalSpeedAverage] != -1000) {
				verticalSpeedLabel = [NSString stringWithFormat:@"%.1fm/s", [currentLocation verticalSpeedAverage]];
			}	
			//We set the glideratio
			if([currentLocation glideRatioAverage] != -1000) {
				glideRatioLabel = [NSString stringWithFormat:@"%.1fm/s", [currentLocation glideRatioAverage]];
			}
		}
	}
	//We refresh the view
	[self refresh];
}

- (void)newError:(NSString *)text {
}

@end
